package com.ankiang.mykeys;


import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.etsy.android.grid.util.DynamicHeightTextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Random;

/***
 * ADAPTER
 */

public class AppAdapter extends ArrayAdapter<JSONObject> {

    private static final String TAG = "AppAdapter";

    static class ViewHolder {
        //DynamicHeightTextView txtShortcutDes;
        public TextView txtAppDes;
        public Button btnAppAddFav;
        public ImageView imgAppImg;
        public Button btnAppUp;
        public TextView txtAppName;

        public String appId;
    }

    public ImageLoader imageLoader; //用来下载图片的类，后面有介绍

    private final LayoutInflater mLayoutInflater;
    private final Random mRandom;
    private final ArrayList<Integer> mBackgroundColors;

    private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();

    public AppAdapter(final Context context, final int textViewResourceId) {
        super(context, textViewResourceId);
        mLayoutInflater = LayoutInflater.from(context);
        mRandom = new Random();
        mBackgroundColors = new ArrayList<>();
        mBackgroundColors.add(R.color.orange);
        mBackgroundColors.add(R.color.green);
        mBackgroundColors.add(R.color.blue);
        mBackgroundColors.add(R.color.yellow);
        mBackgroundColors.add(R.color.grey);
        imageLoader = new ImageLoader(context.getApplicationContext());
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder vh;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.home_list_item, parent, false);
            vh = new ViewHolder();
            //vh.txtShortcutDes = (DynamicHeightTextView) convertView.findViewById(R.id.app_des);
            vh.txtAppDes = (TextView) convertView.findViewById(R.id.app_des);
            vh.btnAppAddFav = (Button) convertView.findViewById(R.id.app_add_fav);
            vh.btnAppUp = (Button) convertView.findViewById(R.id.app_up);
            vh.imgAppImg = (ImageView) convertView.findViewById(R.id.app_img);
            vh.txtAppName = (TextView) convertView.findViewById(R.id.app_name);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        double positionHeight = getPositionRatio(position);
        int backgroundIndex = position >= mBackgroundColors.size() ?
                position % mBackgroundColors.size() : position;

        convertView.setBackgroundResource(mBackgroundColors.get(backgroundIndex));

        Log.d(TAG, "getView position:" + position + " h:" + positionHeight);


        //vh.txtShortcutDes.setHeightRatio(positionHeight);
        JSONObject obj = getItem(position);
        //vh.imgAppImg.setIm
//        if (vh.imgAppImg == null) {
//            Log.e(TAG, "没有找到图片控件");
//        } else {
        String logo = Utils.getStringFromJson(obj, "logo");
        String des = Utils.getStringFromJson(obj, "des");
        String name = Utils.getStringFromJson(obj, "name");
        //imageLoader.DisplayImage(logo, vh.imgAppImg);
        vh.txtAppDes.setText(des);
        vh.txtAppName.setText(name);
        vh.appId = Utils.getStringFromJson(obj, "appid");
        Log.i(TAG, obj.toString());
//        }

        vh.btnAppAddFav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Toast.makeText(getContext(), "Button Clicked Position " +
                        position, Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    private double getPositionRatio(final int position) {
        double ratio = sPositionHeightRatios.get(position, 0.0);
        // if not yet done generate and stash the columns height
        // in our real world scenario this will be determined by
        // some match based on the known height and width of the image
        // and maybe a helpful way to get the column height!
        if (ratio == 0) {
            ratio = getRandomHeightRatio();
            sPositionHeightRatios.append(position, ratio);
            Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
        }
        return ratio;
    }

    private double getRandomHeightRatio() {
        return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5 the width
    }
}